package com.ellricky.excute_president.ui.activity.school

import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ViewUtil
import com.andr.common.util.date.TimeUtil
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.http.callback.EListCallback
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.excute_president.*
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.pd_activity_total_income.*
import java.util.*

class TotalIncomeActivity : BaseToolBarActivity<Void, BasePresenter<Void>>(), TabLayout.OnTabSelectedListener {
    private var month_format = "yyyy-MM"
    private val incomeTypeList = listOf("月统计", "年统计")
    private var monthType = true

    private lateinit var tabList: List<String>
    private var loadFlag: Boolean = false
    private lateinit var curMonth: String
    private lateinit var curYear: String

    private lateinit var adapter: IncomeAdapter

    override fun getContentLayout() = R.layout.pd_activity_total_income

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = getString(R.string.pd_total_income)

        adapter = IncomeAdapter()
        rv.layoutManager = LinearLayoutManager(this)
        rv.adapter = adapter

        tvFront.setOnClickListener(this)
        tvNext.setOnClickListener(this)
        tabLayout.addOnTabSelectedListener(this)

        changeMonthOrYear()
    }

    override fun onClickView(v: View?) {
        super.onClickView(v)
        when (v) {
            tvFront,
            tvNext -> {
                monthType = !monthType
                changeMonthOrYear()
            }
        }
    }


    private fun changeMonthOrYear() {
        tabLayout.removeAllTabs()
        var curTabIndex = 0

        if (monthType) {
            tvTime.text = "月统计"

            tabList = listOf(
                    "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"
            )

            for (i in 0..11) {
                if (i != 0) loadFlag = true
                val childTab = tabLayout.newTab()
                childTab.text = tabList[i]
                tabLayout.addTab(childTab)
            }

            val mon = TimeUtil.getFieldValue(null, Calendar.MONTH)
            curTabIndex = mon - 1

        } else {
            tvTime.text = "年统计"
            val year = TimeUtil.getFieldValue(null, Calendar.YEAR)
            for (i in 0..10) {
                val yeari = "${year + i}年"
                val childTab = tabLayout.newTab()
                childTab.text = yeari
                tabLayout.addTab(childTab)
            }

            curTabIndex = 0
        }

        val childTab = tabLayout.getTabAt(curTabIndex)
        childTab?.select()

        when (monthType) {
            true -> {
                val width = (getOffsetWidth(curTabIndex) * resources.displayMetrics.density)
                tabLayout.post { tabLayout.scrollTo(width.toInt(), 0) }
            }
        }

    }


    override fun onTabReselected(tab: TabLayout.Tab?) {
    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {
        tab?.customView = null
    }

    override fun onTabSelected(tab: TabLayout.Tab?) {
        tvTitle.text = when (monthType) {
            true -> {
                val curYear = "${TimeUtil.getFieldValue(null, Calendar.YEAR)}"
                val curMonthStr = tab?.text?.toString()?.replace("月", "")?.toInt()
                curMonth = curMonthStr?.let {
                    if (it < 10) curYear.plus("-0").plus(it) else curYear.plus("-").plus(it.toString())
                } ?: curYear
                curMonth
            }
            else -> {
                curYear = tab?.text.toString().replace("年", "")
                curYear
            }
        }
        onChangeTab(tab)
    }

    private fun onChangeTab(tab: TabLayout.Tab?) {
        val customeView = ViewUtil.createView(this, R.layout.pd_income_tab_custom)
        val textView = customeView.findViewById<TextView>(R.id.tvValue)
        textView.text = tab?.text
        tab?.customView = customeView
        if (loadFlag) loadData()
    }

    private fun loadData() {
        val paramMap = when (monthType) {
            true -> mapOf("monthDate" to curMonth)
            else -> mapOf("year" to curYear)
        }

        val url = EAppUtil.getApiHost() + when (monthType) {
            true -> API_MONTH_PRICE
            else -> API_YEAR_PRICE
        }

        HttpUtils.getInstance().post(url, paramMap, object : EListCallback<IncomeEntity>(this) {
            override fun onSuccess(data: MutableList<IncomeEntity>?, msg: String?) {
                val dataSource = data ?: listOf<IncomeEntity>()
                adapter.refresh(dataSource)
            }
        })
    }

    private fun getOffsetWidth(index: Int): Int {
        var str = ""
        for (i in 0 until index) {
            str = str.plus(tabList[i])
        }
        return str.length * 14 + index * 12
    }

}